package com.sikaryofficial.qrcode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sikaryofficial.qrcode.domain.entity.BrandTaste;
import com.sikaryofficial.qrcode.mapper.BrandTasteMapper;
import com.sikaryofficial.qrcode.service.IBrandTasteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 品牌口味Service业务层处理
 *
 * @author qinjinyuan
 * @date 2023-10-27
 */
@Service
public class BrandTasteServiceImpl extends ServiceImpl<BrandTasteMapper, BrandTaste> implements IBrandTasteService {
    @Autowired
    private BrandTasteMapper brandTasteMapper;

    /**
     * 更新产品导出最大商品二维码个数
     *
     * @param md5    MD5字符串
     * @param maxNum 二维码生成个数
     * @param userId 用户id
     * @return
     */
    @Override
    public Long updateQrMaxNum(String md5, Long userId, long maxNum) {
        BrandTaste brandTaste = this.getOne(new LambdaQueryWrapper<BrandTaste>()
                .select(BrandTaste::getQrExportNum, BrandTaste::getBrandTasteId)
                .eq(BrandTaste::getMd5, md5)
                .eq(BrandTaste::getDeletedVersion, 0L)
                .last(" limit 1 ")
        );
        // 设置当前最大商品二维码个数
        Long qrExportNum = maxNum + brandTaste.getQrExportNum();
        brandTaste.setQrExportNum(qrExportNum);
        brandTaste.setUpdatedBy(userId);
        brandTaste.setUpdatedTime(new Date());
        this.updateById(brandTaste);
        return qrExportNum;
    }
}
